using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Network;
using MicroMsg.Scene;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace MicroMsg.UI.Page
{
	public class LogView : BasePage
	{
		private const string LOG_FILENAME = "/Log/MicroMsg.log";
		private const string BAK_FILENAME = "/Log/MicroMsg.bak";
		private static ObservableCollection<string> sListLogs;
		private static Dictionary<char, bool> sDictFilter;
		private static int sNBufferSize = 1000;
		private static char[] sArrayBuffer;
		private static bool sBIsInit = false;
		private ObservableCollection<string> mListSource;
		private bool mBIsLoadedData;
		internal Grid LayoutRoot;
		internal StackPanel TitlePanel;
		internal TextBlock ApplicationTitle;
		internal Button button1;
		internal Button button2;
		internal Button button3;
		internal Grid ContentPanel;
		internal ListBox mListBoxLog;
		internal CheckBox checkBoxE;
		internal CheckBox checkBoxW;
		internal CheckBox checkBoxI;
		internal CheckBox checkBoxD;
		internal CheckBox checkBoxV;
		private bool _contentLoaded;
		public static void initStaticMember()
		{
			if (Log.sBIsWriteToLogView && !LogView.sBIsInit)
			{
				LogView.sBIsInit = true;
				LogView.sDictFilter = new Dictionary<char, bool>();
				LogView.sDictFilter.Add('e', true);
				LogView.sDictFilter.Add('w', true);
				LogView.sDictFilter.Add('i', true);
				LogView.sDictFilter.Add('d', true);
				LogView.sDictFilter.Add('v', true);
				LogView.sNBufferSize = 1000;
				return;
			}
		}
		public LogView()
		{
			this.InitializeComponent();
			LogView.initStaticMember();
			base.add_Loaded(new RoutedEventHandler(this.LogView_Loaded));
		}
		private void LogView_Loaded(object sender, RoutedEventArgs e)
		{
			Profiler.printfProfile();
			this.checkBoxE.set_IsChecked(new bool?(LogView.sDictFilter.get_Item('e')));
			this.checkBoxW.set_IsChecked(new bool?(LogView.sDictFilter.get_Item('w')));
			this.checkBoxI.set_IsChecked(new bool?(LogView.sDictFilter.get_Item('i')));
			this.checkBoxD.set_IsChecked(new bool?(LogView.sDictFilter.get_Item('d')));
			this.checkBoxV.set_IsChecked(new bool?(LogView.sDictFilter.get_Item('v')));
			if (!this.mBIsLoadedData)
			{
				this.getLogFromStrBuffer();
				this.refreshSource();
				this.mBIsLoadedData = true;
			}
		}
		private void getLogFromStrBuffer()
		{
			string logString = Log.getLogString();
			if (logString != null)
			{
				char[] array = new char[]
				{
					'\n'
				};
				LogView.sListLogs = logString.Split(new string[]
				{
					"\n"
				}, 1).ToObservableCollection<string>();
				string text = "aaaaa\nbbbbb\n\ncc";
				Enumerable.ToList<string>(text.Split(new string[]
				{
					"\n"
				}, 1));
			}
		}
		private void refreshSource()
		{
			this.mListSource = new ObservableCollection<string>();
			if (LogView.sListLogs == null)
			{
				return;
			}
			using (IEnumerator<string> enumerator = LogView.sListLogs.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					string current = enumerator.get_Current();
					if (current != null && 28 <= current.get_Length())
					{
						char c = current.get_Chars(27);
						if (LogView.sDictFilter.ContainsKey(c) && LogView.sDictFilter.get_Item(c))
						{
							this.mListSource.Add(current);
						}
					}
				}
			}
			this.mListBoxLog.set_ItemsSource(this.mListSource);
			if (this.mListSource.get_Count() > 30)
			{
				Deployment.get_Current().get_Dispatcher().BeginInvoke(delegate
				{
					this.mListBoxLog.ScrollIntoView(this.mListBoxLog.get_Items().get_Item(this.mListBoxLog.get_Items().get_Count() - 1));
				});
			}
			this.mListBoxLog.UpdateLayout();
		}
		private void checkBoxE_Click(object sender, RoutedEventArgs e)
		{
			LogView.sDictFilter.set_Item('e', !LogView.sDictFilter.get_Item('e'));
			this.refreshSource();
		}
		private void checkBoxW_Click(object sender, RoutedEventArgs e)
		{
			LogView.sDictFilter.set_Item('w', !LogView.sDictFilter.get_Item('w'));
			this.refreshSource();
		}
		private void checkBoxI_Click(object sender, RoutedEventArgs e)
		{
			LogView.sDictFilter.set_Item('i', !LogView.sDictFilter.get_Item('i'));
			this.refreshSource();
		}
		private void checkBoxD_Click(object sender, RoutedEventArgs e)
		{
			LogView.sDictFilter.set_Item('d', !LogView.sDictFilter.get_Item('d'));
			this.refreshSource();
		}
		private void checkBoxV_Click(object sender, RoutedEventArgs e)
		{
			LogView.sDictFilter.set_Item('v', !LogView.sDictFilter.get_Item('v'));
			this.refreshSource();
		}
		private void onClickRefresh(object sender, RoutedEventArgs e)
		{
			this.getLogFromStrBuffer();
			this.refreshSource();
		}
		private void getLogFromFile()
		{
			LogView.sListLogs = new ObservableCollection<string>();
			IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication();
			if (userStoreForApplication.FileExists("/Log/MicroMsg.log"))
			{
				IsolatedStorageFileStream isolatedStorageFileStream = userStoreForApplication.OpenFile("/Log/MicroMsg.log", 3, 1);
				using (StreamReader streamReader = new StreamReader(isolatedStorageFileStream))
				{
					while (!streamReader.get_EndOfStream())
					{
						string text = streamReader.ReadLine();
						if (text != null && 19 <= text.get_Length())
						{
							LogView.sListLogs.Insert(0, text);
						}
					}
					streamReader.Close();
					isolatedStorageFileStream.Close();
				}
			}
		}
		private void onClickResetFlow(object sender, RoutedEventArgs e)
		{
			FlowControl.cleanCount();
		}
		private void onClickViewInfo(object sender, RoutedEventArgs e)
		{
			EventCenter.printfInfo();
			FlowControl.printInfo();
			HostService.printInfo();
			Connector.printInfo();
			AppInfoHelper.printfInfo();
			ServiceCenter.pushNotifyMgr.printfInfo();
			Profiler.printfProfile();
			this.onClickRefresh(null, null);
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/LogView/LogView.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.TitlePanel = (StackPanel)base.FindName("TitlePanel");
			this.ApplicationTitle = (TextBlock)base.FindName("ApplicationTitle");
			this.button1 = (Button)base.FindName("button1");
			this.button2 = (Button)base.FindName("button2");
			this.button3 = (Button)base.FindName("button3");
			this.ContentPanel = (Grid)base.FindName("ContentPanel");
			this.mListBoxLog = (ListBox)base.FindName("mListBoxLog");
			this.checkBoxE = (CheckBox)base.FindName("checkBoxE");
			this.checkBoxW = (CheckBox)base.FindName("checkBoxW");
			this.checkBoxI = (CheckBox)base.FindName("checkBoxI");
			this.checkBoxD = (CheckBox)base.FindName("checkBoxD");
			this.checkBoxV = (CheckBox)base.FindName("checkBoxV");
		}
	}
}
